#!/usr/bin/python

from StreamManager import StreamManager
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
import gobject

class PyPlaya:
	def __init__(self):
		self.timeout_id = -1
		self.was_playing = False
		self.streamManager = StreamManager(self.metadata_callback, 
				self.eos_callback)
		self.widgetTree = gtk.glade.XML('playa.glade')

		#Widgets
		self.window = self.widgetTree.get_widget('playaWindow')
		self.playPauseBtn = self.widgetTree.get_widget('playPauseBtn')
		self.playPauseImg = self.widgetTree.get_widget('playPauseImg')
		self.playingLabel = self.widgetTree.get_widget('playingLabel')
		self.timeLabel = self.widgetTree.get_widget('timeLabel')
		self.timeBar = self.widgetTree.get_widget('timeBar')
		self.time_adjustment = self.timeBar.get_adjustment()
		
		self.window.set_title('PyPlaya')
		
		self.widgetTree.signal_autoconnect(self)
		self.window.show_all()

	# Callbacks -----------------------------------------------------
	def on_timeBar_press(self, widget, event):
		self.was_playing = self.streamManager.is_playing()
		if self.was_playing:
			gobject.source_remove(self.timeout_id)
		
	def on_timeBar_release(self, widget, event):
		pos = long(self.timeBar.get_value() * self.streamManager.duration / 100)
		self.streamManager.seek(pos)
		self.update_gui()
		if self.was_playing:
			self.play()

	def on_playPause_clicked(self, widget):
		if self.streamManager.is_playing():
			self.pause()
		else:
			self.play()

	def on_open_clicked(self, widget):
		fileDlg = gtk.FileChooserDialog(title='Abrir arquivo...',
				parent=self.window, action=gtk.FILE_CHOOSER_ACTION_OPEN,
				buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
				gtk.STOCK_OK, gtk.RESPONSE_OK))
		if fileDlg.run() == gtk.RESPONSE_OK:
			self.open_media(fileDlg.get_filename())
		fileDlg.hide()
	
	def metadata_callback(self):
		self.playingLabel.set_markup('<big><b>%s - %s</b></big>' %
				(self.streamManager.artist, self.streamManager.title))

	def eos_callback(self):
		self.pause()
		self.streamManager.seek(long(0))
		self.update_gui(init=True)
		
	# Metodos auxiliares --------------------------------------------
	def play(self):
		if self.streamManager.play():
			self.playPauseImg.set_from_stock(
					gtk.STOCK_MEDIA_PAUSE, 
					gtk.ICON_SIZE_BUTTON)
			self.timeout_id = gobject.timeout_add(500, self.update_gui)

	def pause(self):
		if self.streamManager.pause():
			self.playPauseImg.set_from_stock(
					gtk.STOCK_MEDIA_PLAY, 
					gtk.ICON_SIZE_BUTTON)
			gobject.source_remove(self.timeout_id)
	
	def open_media(self, filepath):
		self.playPauseBtn.set_sensitive(True)
		self.timeBar.set_sensitive(True)
		self.streamManager.open(filepath)
		self.pause()
				
	def update_gui(self, init=False):
		position, duration = self.streamManager.get_position()
		if init:
			position = 0
		self.timeLabel.set_text('%s/%s' % 
				(self.streamManager.format_time(position),
				self.streamManager.format_time(duration)))
		self.time_adjustment.set_value(position * 100.0 / duration)
		return True
				
	def exit_playa(self, widget):
		self.streamManager.pause()
		gtk.main_quit()

# Main ========================================================================
if __name__ == "__main__":
	PyPlaya()
	gtk.main()
